﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using UniversityStudentManagemnetSystem.BLL;
using UniversityStudentManagemnetSystem.DAO.DAL;

namespace UniversityStudentManagemnetSystem.UI
{
    public partial class CoursePage : System.Web.UI.Page
    {
        Course course = new Course();
        CourseManager courseManager = new CourseManager();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!User.Identity.IsAuthenticated)
            {
                Response.Redirect("~/Account/Login.aspx", false);
            }

            if (IsPostBack != true)
            {
                PopulatedDepartmentDropDownList();
                PopulatedSemesterDropDownList();
            }
        }

        private void PopulatedSemesterDropDownList()
        {
            SemesterManager semesterManager = new SemesterManager();
            DataTable semesters = semesterManager.GetAllSemester();
            semesterDropDownList.DataTextField = "SemesterName";
            semesterDropDownList.DataValueField = "Id";
            semesterDropDownList.DataSource = semesters;
            semesterDropDownList.DataBind();
        }

        private void PopulatedDepartmentDropDownList()
        {
            DepartmentManager departmentManager = new DepartmentManager();
            DataTable departments = departmentManager.GetAllDepartments();
            departmentDropDownList.DataTextField = "Name";
            departmentDropDownList.DataValueField = "Id";
            departmentDropDownList.DataSource = departments;
            departmentDropDownList.DataBind();
        }

        protected void courseEntrySaveButton_Click(object sender, EventArgs e)
        {
            if (codeTextBox.Text != "" && creditTextBox.Text != "" && nameTextBox.Text != "" && descriptionTextBox.Text != "")
            {
                try
                {
                    course.Code = codeTextBox.Text;
                    course.Credit =creditTextBox.Text;
                    course.CourseName = nameTextBox.Text;
                    course.Description = descriptionTextBox.Text;
                    course.Department.DeptId = Convert.ToInt32(departmentDropDownList.SelectedValue);
                    course.Semester.Id = Convert.ToInt32(semesterDropDownList.SelectedValue);
                    if (courseManager.SaveCourse(course))
                    {
                        courseEntryMessageLabel.ForeColor = Color.Green;
                        courseEntryMessageLabel.Text = "Saved successfully.";
                    }
                }
                catch (FormatException formatException)
                {
                    courseEntryMessageLabel.ForeColor = Color.Red;
                    courseEntryMessageLabel.Text = formatException.Message;
                }
                catch (Exception exception)
                {

                    courseEntryMessageLabel.ForeColor = Color.Red;
                    courseEntryMessageLabel.Text = exception.Message;
                }
            }
            else
            {
                courseEntryMessageLabel.ForeColor = Color.BlueViolet;
                courseEntryMessageLabel.Text = "Please fill up the all fields.";
            }
            ClearTextBoxes();
        }

        private void ClearTextBoxes()
        {
            codeTextBox.Text = string.Empty;
            creditTextBox.Text = string.Empty;
            nameTextBox.Text = string.Empty;
            descriptionTextBox.Text = string.Empty;
        }
    }
}